<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\Account as AccountModel;
use app\common\model\AccountBonus;
use app\common\model\Assets;
use app\common\tool\account\AccountException;
use think\facade\Event;

class AccountHandle
{
    public function handle($handle)
    {
        try {
            $bonusTotal = 0;
            $real = 0;
            if (is_array($handle->account)) {
                if (!is_array($handle->amount) || count($handle->account) != count($handle->amount)) {
                    throw new AccountException('参数account和amount不对应', '0');
                }
                $assets = Assets::where('uid', $handle->uid)->find();
                foreach ($handle->account as $k => $v) {
                    $after = $handle->fx == '+' ? floatval($assets->$v) + floatval($handle->amount[$k]) :
                        floatval($assets->$v) - floatval($handle->amount[$k]);
                    $data[] = [
                        'uid' => $handle->uid,
                        'type' => $v,
                        'intro' => $handle->intro,
                        'amount' => $handle->amount[$k],
                        'front' => $assets->$v,
                        'after' => $after,
                        'fx' => $handle->fx,
                        'create_time' => time(),
                        'create_date' => date('Y-m-d'),
                        'source' => $handle->source ?? 0,
                        'msg' => $handle->msg,
                        'is_bonus' => $handle->type == 2 ? 1 : 0,
                        'is_admin' => $handle->type == 3 ? 1 : 0,
                        'is_grant' => $handle->is_grant,
                        'is_user' => $handle->is_user,
                        'category' => $handle->category ?? 0,
                    ];
                    if ($handle->type == 2) $bonusTotal += floatval($handle->amount[$k]);
                    if ($v == 'bonus') {
                        $real += floatval($handle->amount[$k]);
                    }
                    if ($handle->update) $assets->$v = $after;

                }
                if ($handle->update) $assets->save();
            } else {
                $account = $handle->account;
                $assets = Assets::where('uid', $handle->uid)->find();

                $after = $handle->fx == '+' ? floatval($assets->$account) + floatval($handle->amount) :
                    floatval($assets->$account) - floatval($handle->amount);
                $data[] = [
                    'uid' => $handle->uid,
                    'type' => $account,
                    'intro' => $handle->intro,
                    'amount' => $handle->amount,
                    'front' => $assets->$account,
                    'after' => $after,
                    'fx' => $handle->fx,
                    'create_time' => time(),
                    'create_date' => date('Y-m-d'),
                    'source' => $handle->source ?? 0,
                    'msg' => $handle->msg,
                    'is_bonus' => $handle->type == 2 ? 1 : 0,
                    'is_admin' => $handle->type == 3 ? 1 : 0,
                    'is_grant' => $handle->is_grant,
                    'is_user' => $handle->is_user,
                    'category' => $handle->category ?? 0
                ];

                if ($handle->update) $assets->$account = $after;
                if ($handle->update) $assets->save();

                if ($handle->category > 0) {
                    Event::trigger('Summary', ['key' => 'sum_' . $handle->category, 'value' => $handle->amount, 'uid' => $handle->uid]);
                }
            }
            if (!empty($data)) {
                (new AccountModel())->saveAll($data);
            }

            return true;

        } catch (\Exception $e) {

            throw new AccountException($e->getMessage(), '0');
        }
    }


}